import { defineStore } from 'pinia'
import store from './index'
import { ElMessage } from 'element-plus'

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useUser = defineStore('user', {
  // other options...
  state:()=>{
    const cacheUser = localStorage.getItem('currentUser')?JSON.parse(localStorage.getItem('currentUser')) : {}
    return {
        currentUser:{
          id:cacheUser.id || '',
          name:cacheUser.name || '',
          phone:cacheUser.phone || '',
          roleName:cacheUser.roleName || '',
          avatar:cacheUser.avatar || '',
          ...cacheUser
        },
    }
  },
  actions: {
    changeCurrentUser(currentUser){
      console.log('currentUser: ', currentUser);
      this.currentUser = currentUser
      localStorage.setItem('currentUser',JSON.stringify(currentUser))
    }
  }
})